package com.springboot.limitSpring.dome;

import com.springboot.limitSpring.model.LimitRateResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LimitrateInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(LimitrateInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        RateLimiter rateLimiter = RateLimiter.getInstance();
        LimitRateResult limitRateResult = rateLimiter.acquire();
        System.out.println(limitRateResult.massage);
        if (limitRateResult.state == 200) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

}


